using System;
using System.Collections.Generic;
using System.Text;

using Google.GData.Client;
using Google.GData.Contacts;
using Google.GData.Extensions;


namespace Hansvits.MyGoogle.Contacts
{
    public static class ContactManager
    {
        static string _username;
        static string _password;
        static string _authenticationToken;

        static ContactsService _service;
        static ContactsFeed _feed;

        static ContactManager()
        {
            _authenticationToken = string.Empty;
            _service = new ContactsService("Hansvits.gContacts.1.0");
        }

        public static bool Authenticate(string username, string password)
        {
            _username = username;
            _password = password;
            return Authenticate();
        }

        static bool Authenticate()
        {
            bool result = false;
            _service.setUserCredentials(_username, _password);
            try
            {
                _authenticationToken = _service.QueryAuthenticationToken();
                result = true;
            }
            catch (AuthenticationException aex)
            {
                result = false;
                Logger.Log(aex);
            }
            catch (Google.GData.Client.GDataRequestException rex)
            {
                result = false;
                Logger.Log(rex);
            }
            return result;            
        }

        public static void FetchContacts()
        {
            if (_authenticationToken.Length <= 0)
                Authenticate();
            ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
            _feed = _service.Query(query);
            if (_feed.TotalResults > 25)
            {
                query.NumberToRetrieve = _feed.TotalResults;
                _feed = _service.Query(query);
            }

            #if DEBUG
                _feed.SaveToXml(new System.IO.FileStream(@"C:\myContacts.xml", System.IO.FileMode.Create));
            #endif
        }

        public static AtomEntryCollection Entries
        {
            get
            {
                if (_feed == null)
                {
                    FetchContacts();
                }
                return _feed.Entries;
            }
        }

        public static bool Publish()
        {
            try
            {
                _feed.Publish();
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
            
            return true;
        }

        public static ContactEntry Publish(ContactEntry entry)
        {
            ContactEntry updated = null;
            if (entry.Id.Uri != null)
            {
                updated = (ContactEntry) entry.Update();
            }
            else
            {
                updated = (ContactEntry)_feed.Insert(entry);
                _feed.Entries.Add(updated);
            }

            return updated;
        }

        public static string GetRelationship(string data)
        {
            string result = ContactsRelationships.IsGeneral;
            switch (data)
            {
                case "car":
                    result = ContactsRelationships.IsCar;
                    break;
                case "fax":
                    result = ContactsRelationships.IsFax;
                    break;
                case "general":
                    result = ContactsRelationships.IsGeneral;
                    break;
                case "home":
                    result = ContactsRelationships.IsHome;
                    break;
                case "home fax":
                    result = ContactsRelationships.IsHomeFax;
                    break;
                case "internal extension":
                    result = ContactsRelationships.IsInternalExtension;
                    break;
                case "mobile":
                    result = ContactsRelationships.IsMobile;
                    break;
                case "other":
                    result = ContactsRelationships.IsOther;
                    break;
                case "pager":
                    result = ContactsRelationships.IsPager;
                    break;
                case "satellite":
                    result = ContactsRelationships.IsSatellite;
                    break;
                case "voip":
                    result = ContactsRelationships.IsVoip;
                    break;
                case "work":
                    result = ContactsRelationships.IsWork;
                    break;
                case "work fax":
                    result = ContactsRelationships.IsWorkFax;
                    break;
                default:
                    result = ContactsRelationships.IsOther;
                    break;
            }

            return result;
        }

        public static string GetLookup(string data)
        {
            string result = "";
            switch (data)
            {
                case "http://schemas.google.com/g/2005#car":
                    result = "car";
                    break;
                case "http://schemas.google.com/g/2005#fax":
                    result = "fax";
                    break;
                case "http://schemas.google.com/g/2005#general":
                    result = "general";
                    break;
                case "http://schemas.google.com/g/2005#home":
                    result = "home";
                    break;
                case "http://schemas.google.com/g/2005#home_fax":
                    result = "home fax";
                    break;
                case "http://schemas.google.com/g/2005#internal-extension":
                    result = "internal extension";
                    break;
                case "http://schemas.google.com/g/2005#mobile":
                    result = "mobile";
                    break;
                case "http://schemas.google.com/g/2005#other":
                    result = "other";
                    break;
                case "http://schemas.google.com/g/2005#pager":
                    result = "pager";
                    break;
                case "http://schemas.google.com/g/2005#satellite":
                    result = "satellite";
                    break;
                case "http://schemas.google.com/g/2005#voip":
                    result = "voip";
                    break;
                case "http://schemas.google.com/g/2005#work":
                    result = "work";
                    break;
                case "http://schemas.google.com/g/2005#work_fax":
                    result = "work fax";
                    break;
                default:
                    result = "other";
                    break;
            }

            return result;
        }
    }
}
